#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include "mutex.h"

struct c_mutex {
	pthread_mutex_t mutex;
};

int c_mutex_create(c_mutex_t *mutex, const char *name)
{
	int ret = -1;

	(void)name;
	struct c_mutex *mutex_ptr = (struct c_mutex *)malloc(sizeof(*mutex_ptr));
	if (mutex_ptr == NULL) {
		return -1;
	}
	
	ret = pthread_mutex_init(&mutex_ptr->mutex, NULL);
	if (ret != 0) {
		free(mutex_ptr);
		return -1;
	}

	*mutex = mutex_ptr;
	return 0;
}

int c_mutex_delete(c_mutex_t *mutex)
{
	pthread_mutex_destroy(&(*mutex)->mutex);
	free(*mutex);
	return 0;
}

int c_mutex_lock(c_mutex_t *mutex)
{
	return pthread_mutex_lock(&(*mutex)->mutex);
}

int c_mutex_unlock(c_mutex_t *mutex)
{
	return pthread_mutex_unlock(&(*mutex)->mutex);
}
